cmake_minimum_required(VERSION 2.6)

project(cthrift C CXX)

IF(ENABLE_COVERAGE)
SET(CMAKE_CXX_FLAGS "-g -Wall -O0 -fno-strict-aliasing -fPIC -DMUDUO_STD_STRING -DHAVE_NETINET_IN_H")
ELSE()
SET(CMAKE_CXX_FLAGS "-g -Wall -O2 -fno-strict-aliasing -fPIC -DMUDUO_STD_STRING -DHAVE_NETINET_IN_H")
ENDIF()

MESSAGE(STATUS "CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}")

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
MESSAGE(STATUS "CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}")

IF(ENABLE_COVERAGE)
    INCLUDE(CodeCoverage)
	APPEND_COVERAGE_COMPILER_FLAGS()
ENDIF()

if (NOT CMAKE_BUILD_NO_TESTS)
    enable_testing()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -fprofile-arcs -ftest-coverage")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  -fprofile-arcs -ftest-coverage")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}  -fprofile-arcs -ftest-coverage-lgcov")
ENDIF()

if (CMAKE_BUILD_INIT)
    FIND_PACKAGE(Muduo REQUIRED)
else()
    find_package(Boost REQUIRED)
    find_package(ZLIB REQUIRED)
    find_path(LOG4CPLUS_INCLUDE_DIR log4cplus)
    find_library(LOG4CPLUS_LIBRARY NAMES log4cplus)
    find_path(ZOOKEEPER_INCLUDE_DIR zookeeper)
    find_library(ZOOKEEPER_LIBRARY libzookeeper_mt.a)
    find_program(THRIFT_COMPILER thrift)
    find_path(THRIFT_INCLUDE_DIR thrift/Thrift.h)
    find_library(THRIFT_LIBRARY NAMES thrift)
    find_library(MUDUO_BASE_LIBRARY libmuduo_base.a)
    find_library(MUDUO_NET_LIBRARY libmuduo_net.a)
    find_library(MUDUO_HTTP_LIBRARY libmuduo_http.a)
    find_path(MUDUO_INCLUDE_DIR muduo)

    if(ZLIB_FOUND)
        MESSAGE(STATUS "found zlib  ${ZLIB_FOUND}")
    else()
        MESSAGE(STATUS "no found zlib")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(THRIFT_COMPILER AND THRIFT_INCLUDE_DIR AND THRIFT_LIBRARY)
        MESSAGE(STATUS "found thrift")
    else()
        MESSAGE(STATUS "no found thrift")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(MUDUO_BASE_LIBRARY AND MUDUO_NET_LIBRARY AND MUDUO_HTTP_LIBRARY AND MUDUO_INCLUDE_DIR)
        MESSAGE(STATUS "found muduo")
    else()
        MESSAGE(STATUS "no found muduo")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(LOG4CPLUS_INCLUDE_DIR AND LOG4CPLUS_LIBRARY)
        MESSAGE(STATUS "found log4cplus")
    else()
        MESSAGE(STATUS "no found log4cplus")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(ZOOKEEPER_INCLUDE_DIR AND ZOOKEEPER_LIBRARY)
        MESSAGE(STATUS "found zookeeper")
    else()
        MESSAGE(STATUS "no found zookeeper")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(Boost_INCLUDE_DIRS)
        MESSAGE(STATUS "found boost")
    else()
        MESSAGE(STATUS "no found boost")
        SET(SOME_NO_FOUND TRUE)
    endif()

    if(SOME_NO_FOUND)
        MESSAGE(FATAL_ERROR "Dependency not found please confirm")
    endif()

    MESSAGE(STATUS "THRIFT_COMPILER         ${THRIFT_COMPILER}")
    MESSAGE(STATUS "THRIFT_INCLUDE_DIR      ${THRIFT_INCLUDE_DIR}")
    MESSAGE(STATUS "LOG4CPLUS_INCLUDE_DIR   ${LOG4CPLUS_INCLUDE_DIR}")
    MESSAGE(STATUS "ZOOKEEPER_INCLUDE_DIR   ${ZOOKEEPER_INCLUDE_DIR}")
    MESSAGE(STATUS "ZLIB_INCLUDE_DIR        ${ZLIB_INCLUDE_DIR}")
    MESSAGE(STATUS "Boost_INCLUDE_DIRS      ${Boost_INCLUDE_DIRS}")
    MESSAGE(STATUS "ZLIB_LIBRARY            ${ZLIB_LIBRARY}")
    MESSAGE(STATUS "ZOOKEEPER_LIBRARY       ${ZOOKEEPER_LIBRARY}")
    MESSAGE(STATUS "THRIFT_LIBRARY          ${THRIFT_LIBRARY}")
    MESSAGE(STATUS "LOG4CPLUS_LIBRARY       ${LOG4CPLUS_LIBRARY}")
    MESSAGE(STATUS "Boost_INCLUDE_DIRS      ${Boost_INCLUDE_DIRS}")
    MESSAGE(STATUS "MUDUO_BASE_LIBRARY      ${MUDUO_BASE_LIBRARY}")
    MESSAGE(STATUS "MUDUO_NET_LIBRARY       ${MUDUO_NET_LIBRARY}")
    MESSAGE(STATUS "MUDUO_HTTP_LIBRARY      ${MUDUO_HTTP_LIBRARY}")


    INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
    INCLUDE_DIRECTORIES(${THRIFT_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${THRIFT_INCLUDE_DIR}/thrift)
    INCLUDE_DIRECTORIES(${LOG4CPLUS_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${ZOOKEEPER_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})
    INCLUDE_DIRECTORIES(${MUDUO_INCLUDE_DIR})



    SET(CTHRIFT_SRC_PATH ${CMAKE_SOURCE_DIR})
    MESSAGE(STATUS "CTHRIFT_SRC_PATH ${CTHRIFT_SRC_PATH}")

    #for output
    EXECUTE_PROCESS(COMMAND mkdir ${CMAKE_SOURCE_DIR}/build)
    SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/build)
    MESSAGE(STATUS "CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}")

    EXECUTE_PROCESS(COMMAND mkdir ${CMAKE_INSTALL_PREFIX}/include)
    SET(INCLUDE_OUTPUT_PATH ${CMAKE_INSTALL_PREFIX}/include)
    MESSAGE(STATUS "INCLUDE_OUTPUT_PATH ${INCLUDE_OUTPUT_PATH}")

    EXECUTE_PROCESS(COMMAND mkdir ${CMAKE_INSTALL_PREFIX}/lib)
    SET(LIBRARY_OUTPUT_PATH ${CMAKE_INSTALL_PREFIX}/lib)
    MESSAGE(STATUS "LIBRARY_OUTPUT_PATH ${LIBRARY_OUTPUT_PATH}")

    EXECUTE_PROCESS(COMMAND mkdir ${CMAKE_INSTALL_PREFIX}/bin)
    SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_INSTALL_PREFIX}/bin)
    MESSAGE(STATUS "EXECUTABLE_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH}")

    EXECUTE_PROCESS(COMMAND rm -rf ${INCLUDE_OUTPUT_PATH}/cthrift/*)
    EXECUTE_PROCESS(COMMAND mkdir ${INCLUDE_OUTPUT_PATH}/cthrift)
    SET(INCLUDE_CTHRIFT_OUTPUT_PATH ${INCLUDE_OUTPUT_PATH}/cthrift)
    MESSAGE(STATUS "INCLUDE_CTHRIFT_OUTPUT_PATH ${INCLUDE_CTHRIFT_OUTPUT_PATH}")

    EXECUTE_PROCESS(COMMAND mkdir ${INCLUDE_OUTPUT_PATH}/cthrift/util)
    SET(INCLUDE_UTIL_OUTPUT_PATH ${INCLUDE_OUTPUT_PATH}/cthrift/util)
    MESSAGE(STATUS "INCLUDE_UTIL_OUTPUT_PATH ${INCLUDE_UTIL_OUTPUT_PATH}")

    INCLUDE_DIRECTORIES(system ${CMAKE_SOURCE_DIR}/thrid/include)
    INCLUDE_DIRECTORIES(system ${CMAKE_SOURCE_DIR}/thrid/include/thrift)

    LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/thrid/lib/)

    ADD_SUBDIRECTORY(cthrift)

    if (NOT CMAKE_BUILD_NO_EXAMPLES)
        ADD_SUBDIRECTORY(example)
    endif ()
endif()


